/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.nodetype.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.site.manager.nodetype.pojo.*;
import com.jzwl.system.base.dao.BaseDAO;



@Repository("siteNodeTypeDao")
public class SiteNodeTypeDao {
	
	@Autowired
	private Constants constants;
	
	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库
	
	
	public boolean addSiteNodeType(Map<String, Object> map) {
		
		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id",  Sequence.nextId());
		map.put("createTime", new Date());
		map.put("isDelete",  0);
		
		String sql = "insert into " + constants.getCurrentDB() + "site_node_type " 
				 + " (id,name,ord,createTime,isDelete) " 
				 + " values "
				 + " (:id,:name,:ord,:createTime,:isDelete)";
		
		return baseDAO.executeNamedCommand(sql, map);
	}
	
	
	public String getColumns() {
		return ""
				+" t.id as id,"
				+" t.name as name,"
				+" t.ord as ord,"
				+" t.createTime as createTime "
				;
	}
	

	public PageObject querySiteNodeTypeList(Map<String,Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性
		
		String sql="select " + getColumns() + " from " + constants.getCurrentDB() + "site_node_type t where 1=1 ";
		
		  	if(null !=map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())){
			  		sql=sql+ " and t.name  = " + map.get("name") +"";
		  	}
		  	if(null !=map.get("createTime") && StringUtils.isNotEmpty(map.get("createTime").toString())){
			  		sql=sql+ " and t.createTime >= " + map.get("createTimeBegin") +"";
			  		sql=sql+ " and t.createTime <= " + map.get("createTimeEnd"  ) +"";
		  	}
		  	if(null !=map.get("isDelete") && StringUtils.isNotEmpty(map.get("isDelete").toString())){
			  		sql=sql+ " and t.isDelete  = " + map.get("isDelete") +"";
		  	}
					
					
		sql=sql+ " order by ord ";
		
		PageObject po = baseDAO.queryForMPageList(sql, new Object[]{},map);
		
		return po;
	}

	public boolean updateSiteNodeType(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_node_type set ";
				  	if(null !=map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())){
					  	sql=sql+ "			name=:name ,";
				  	}
				  	
				  	if(null !=map.get("ord") && StringUtils.isNotEmpty(map.get("ord").toString())){
					  	sql=sql+ "			ord=:ord ,";
				  	}
				  	
				  	if(null !=map.get("createTime") && StringUtils.isNotEmpty(map.get("createTime").toString())){
					  	sql=sql+ "			createTime=:createTime ,";
				  	}
				sql = sql.substring(0, sql.length()-1);
				sql = sql + " where id=:id";
		return  baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteNodeType(Map<String, Object> map) {
		
		String sql = "delete from " + constants.getCurrentDB() + "site_node_type where id in ("+ map.get("id")+ " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteNodeType(Map<String, Object> map) {
		
		String sql ="update " + constants.getCurrentDB() + "site_node_type set "
				+ " isDelete = 1"
				+ " where id=:id";
		
		return  baseDAO.executeNamedCommand(sql, map);
	}
	
	
	@SuppressWarnings("unchecked")
	public SiteNodeType getById(Map<String, Object> map) {
		
		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_node_type t where t.id = "+ map.get("id") + "";
		
		SiteNodeType siteNodeType=new SiteNodeType();
		
		Map m=baseDAO.queryForMap(sql);
		
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(siteNodeType, m);
		} catch (Exception e) {
			
			e.printStackTrace();
			
			return null;
		}
		
		return siteNodeType;
	}
	

	
	public List<Map<String, Object>> querySiteNodeTypeAll() {
		String sql="select t.id,t.name from " + constants.getCurrentDB() + "site_node_type t ";
		sql=sql+ "  where t.isDelete = 0 order by ord ";
		
		return baseDAO.queryForList(sql);
	}
}
